package com.design.document.controller;

import com.design.document.annotation.Auth;
import com.design.document.common.Const;
import com.design.document.common.ResponseService;
import com.design.document.dto.UserDTO;
import com.design.document.entity.User;
import com.design.document.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@RequestMapping("/user")
@Controller
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/getLogin")
    @ResponseBody
    @Auth //开发者模式下注释掉
    public ResponseService<User> toLogin(@RequestBody UserDTO userDTO) {

        ResponseService<User> response = userService.login(userDTO.getUserName(),userDTO.getPassword());
        if(response.isSuccess()){
           userDTO.setUserStatus(Const.LoginStatus.ON_LINE);
           logger.info("[user = "+userDTO+"]");
        }else {
            logger.error("user不存在");
        }
        return response;
    }


    public static final String DEFAULT_BOOK_QUEUE = "dev.register.default.queue";
    //rabbit_mq设置自动提交ack的route_key
    public static final String MANUAL_BOOK_QUEUE = "dev.register.manual.queue";
    //rabbit_mq设置手动提交ack的route_key

//    @Autowired
//    private RabbitUtil rabbitUtil;


    @RequestMapping(value = "/books",method = RequestMethod.GET)
    @ResponseBody
    public String defaultMessage(){
        User user = new User();

        user.setId(1);
        user.setUsername("一起学springboot");
//        rabbitUtil.convertAndSend(DEFAULT_BOOK_QUEUE,user);
//        rabbitUtil.convertAndSend(MANUAL_BOOK_QUEUE,user);

        return"Success!";
    }

    @RequestMapping(value = "/index")
    public String index(){
        logger.info("进入index页面");
        return "index";
    }


}
